using Microsoft.Phone.Controls.LocalizedResources;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Microsoft.Phone.Controls
{
	public class RecurringDaysPicker : ListPicker
	{
		private const string CommaSpace = ", ";
		private const string EnglishLanguage = "en";
		private string[] DayNames = CultureInfo.get_CurrentCulture().get_DateTimeFormat().get_DayNames();
		private string[] ShortestDayNames = CultureInfo.get_CurrentCulture().get_DateTimeFormat().get_ShortestDayNames();
		public RecurringDaysPicker()
		{
			if (CultureInfo.get_CurrentCulture().get_Name().StartsWith("en", 5))
			{
				this.ShortestDayNames = new string[]
				{
					"Sun",
					"Mon",
					"Tue",
					"Wed",
					"Thu",
					"Fri",
					"Sat"
				};
			}
			DayOfWeek firstDayOfWeek = CultureInfo.get_CurrentCulture().get_DateTimeFormat().get_FirstDayOfWeek();
			for (int i = 0; i < Enumerable.Count<string>(this.DayNames); i++)
			{
				base.get_Items().Add(this.DayNames[(firstDayOfWeek + i) % Enumerable.Count<string>(this.DayNames)]);
			}
			base.SelectionMode = 1;
			base.SummaryForSelectedItemsDelegate = new Func<IList, string>(this.SummarizeDaysOfWeek);
		}
		protected string SummarizeDaysOfWeek(IList selection)
		{
			string result = ControlResources.RepeatsOnlyOnce;
			if (selection != null)
			{
				List<string> list = new List<string>();
				IEnumerator enumerator = selection.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						object current = enumerator.get_Current();
						list.Add((string)current);
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
				result = this.DaysOfWeekToString(list);
			}
			return result;
		}
		private string DaysOfWeekToString(List<string> daysList)
		{
			List<string> list = new List<string>();
			using (List<string>.Enumerator enumerator = daysList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (!list.Contains(current))
					{
						list.Add(current);
					}
				}
			}
			if (list.get_Count() == 0)
			{
				return ControlResources.RepeatsOnlyOnce;
			}
			StringBuilder stringBuilder = new StringBuilder();
			IEnumerable<string> enumerable;
			stringBuilder.Append(RecurringDaysPicker.HandleGroups(list, out enumerable));
			if (stringBuilder.get_Length() > 0)
			{
				stringBuilder.Append(", ");
			}
			DayOfWeek firstDayOfWeek = CultureInfo.get_CurrentCulture().get_DateTimeFormat().get_FirstDayOfWeek();
			for (int i = 0; i < Enumerable.Count<string>(this.DayNames); i++)
			{
				int num = (firstDayOfWeek + i) % Enumerable.Count<string>(this.DayNames);
				string text = this.DayNames[num];
				if (Enumerable.Contains<string>(enumerable, text))
				{
					stringBuilder.Append(this.ShortestDayNames[num]);
					stringBuilder.Append(", ");
				}
			}
			StringBuilder expr_EA = stringBuilder;
			expr_EA.set_Length(expr_EA.get_Length() - ", ".get_Length());
			return stringBuilder.ToString();
		}
		private static string HandleGroups(List<string> days, out IEnumerable<string> unhandledDays)
		{
			if (days.get_Count() == 7)
			{
				unhandledDays = new List<string>();
				return ControlResources.RepeatsEveryDay;
			}
			ReadOnlyCollection<string> weekdays = CultureInfo.get_CurrentCulture().Weekdays();
			ReadOnlyCollection<string> weekends = CultureInfo.get_CurrentCulture().Weekends();
			if (Enumerable.Count<string>(Enumerable.Intersect<string>(days, weekdays)) == weekdays.get_Count())
			{
				unhandledDays = Enumerable.Where<string>(days, (string day) => !weekdays.Contains(day));
				return ControlResources.RepeatsOnWeekdays;
			}
			if (Enumerable.Count<string>(Enumerable.Intersect<string>(days, weekends)) == weekends.get_Count())
			{
				unhandledDays = Enumerable.Where<string>(days, (string day) => !weekends.Contains(day));
				return ControlResources.RepeatsOnWeekends;
			}
			unhandledDays = days;
			return string.Empty;
		}
	}
}
